﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class ContactsController : Controller
    {
        private atletisEntities db = new atletisEntities();

        // GET: /Contacts/
        public ViewResult Index(string clubId, string searchstring)
        {
            var contacts = db.Contacts.Include("Clubs").OrderBy(c => c.Surname).ToList();

            if (!string.IsNullOrEmpty(clubId))
            {
                contacts = contacts.Where(c => c.ClubId == clubId).ToList();
            }
                        
            if (!string.IsNullOrEmpty(searchstring))
            {
                contacts = contacts.Where(c => c.Surname.Contains(searchstring)).ToList();
            }
            
            return View(contacts);
        }

        // GET: /Contacts/Details/5
        public ViewResult Details(int id)
        {
            Contacts contacts = db.Contacts.Single(c => c.ID == id);
            return View(contacts);
        }

        // GET: /Contacts/Create
        public ActionResult Create()
        {
            var clubs = db.Clubs.OrderBy(c => c.City).ToList();
            ViewBag.ClubId = new SelectList(clubs, "ID", "Name");

            return View();
        } 

        // POST: /Contacts/Create
        [HttpPost]
        public ActionResult Create(Contacts contacts)
        {
            if (ModelState.IsValid)
            {
                db.Contacts.AddObject(contacts);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            var clubs = db.Clubs.OrderBy(c => c.City).ToList();
            ViewBag.ClubId = new SelectList(clubs, "ID", "Name", contacts.ClubId);

            return View(contacts);
        }
        
        // GET: /Contacts/Edit/5
        public ActionResult Edit(int id)
        {
            Contacts contacts = db.Contacts.Single(c => c.ID == id);
            
            var clubs = db.Clubs.OrderBy(c => c.City).ToList();
            ViewBag.ClubId = new SelectList(clubs, "ID", "Name", contacts.ClubId);
            
            return View(contacts);
        }

        // POST: /Contacts/Edit/5
        [HttpPost]
        public ActionResult Edit(Contacts contacts)
        {
            if (ModelState.IsValid)
            {
                db.Contacts.Attach(contacts);
                db.ObjectStateManager.ChangeObjectState(contacts, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            
            var clubs = db.Clubs.OrderBy(c => c.City).ToList();
            ViewBag.ClubId = new SelectList(clubs, "ID", "Name", contacts.ClubId);

            return View(contacts);
        }

        // GET: /Contacts/Delete/5
        public ActionResult Delete(int id)
        {
            Contacts contacts = db.Contacts.Single(c => c.ID == id);
            return View(contacts);
        }

        // POST: /Contacts/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Contacts contacts = db.Contacts.Single(c => c.ID == id);
            db.Contacts.DeleteObject(contacts);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}